package com.wdb.common.util;

import org.springframework.util.ObjectUtils;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * this utils just working in web environment
 *
 * @author shiyu.long
 */

public abstract class HttpServletRequestUtils {
    /**
     * get http servlet request
     *
     * @return {@link HttpServletRequest}
     * @see HttpServletRequest
     */
    public static HttpServletRequest getRequest() {
        RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
        if (!ObjectUtils.isEmpty(requestAttributes)) {
            ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) requestAttributes;
            return servletRequestAttributes.getRequest();
        }
        return null;
    }

    /**
     * get http servlet response
     *
     * @return {@link HttpServletResponse}
     * @see HttpServletResponse
     */
    public static HttpServletResponse getResponse() {
        RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
        if (!ObjectUtils.isEmpty(requestAttributes)) {
            ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) requestAttributes;
            return servletRequestAttributes.getResponse();
        }
        return null;
    }

    /**
     * get current http servlet request uri
     *
     * @return current request uri
     */
    public static String getRequestUri() {
        RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
        if (!ObjectUtils.isEmpty(requestAttributes)) {
            ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) requestAttributes;
            return servletRequestAttributes.getRequest().getRequestURI();
        }
        return null;
    }

    /**
     * get current http servlet request params
     */

    public static Map<String, String> getRequestParams() {
        Map<String, String> params = new HashMap<>();
        RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
        if (!ObjectUtils.isEmpty(requestAttributes)) {
            ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) requestAttributes;
            HttpServletRequest request = servletRequestAttributes.getRequest();
            if (Objects.nonNull(request)) {
                Enumeration<String> names = request.getParameterNames();
                while (names.hasMoreElements()) {
                    String name = names.nextElement();
                    params.put(name, request.getParameter(name));
                }
            }
        }
        return params;
    }

    /**
     * get current http servlet request header
     *
     * @return request header
     */
    public static String getRequestHeader(String header) {
        RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
        if (!ObjectUtils.isEmpty(requestAttributes)) {
            ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) requestAttributes;
            return servletRequestAttributes.getRequest().getHeader(header);
        }
        return null;
    }

    /**
     * get http session
     *
     * @return {@link HttpSession}
     * @see HttpServletRequest
     */
    public static HttpSession getSession() {
        RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
        if (!ObjectUtils.isEmpty(requestAttributes)) {
            ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) requestAttributes;
            return servletRequestAttributes.getRequest().getSession();
        }
        return null;
    }

    /**
     * get request parameters
     */
    public static Map<String, Object> getParameters() {
        RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
        if (!ObjectUtils.isEmpty(requestAttributes)) {
            ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) requestAttributes;
            HttpServletRequest request = servletRequestAttributes.getRequest();
            Enumeration<String> names = request.getParameterNames();
            Map<String, Object> parameters = new HashMap<>();
            while (names.hasMoreElements()) {
                String name = names.nextElement();
                parameters.put(name, request.getParameter(name));
            }
            return parameters;
        }
        return new HashMap<>();
    }
}
